package com.dcy.znoa.service.sys.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.publiz.model.SelectBaseModel;
import com.dcy.znoa.controller.sys.model.urlList.SelectUrlListModel;
import com.dcy.znoa.dao.sys.OperRoleDao;
import com.dcy.znoa.dao.sys.RoleAuthDao;
import com.dcy.znoa.dao.sys.UrlListDao;
import com.dcy.znoa.dao.user.OperDao;
import com.dcy.znoa.domain.Oper;
import com.dcy.znoa.domain.sys.OperRole;
import com.dcy.znoa.domain.sys.RoleAuth;
import com.dcy.znoa.domain.sys.UrlList;
import com.dcy.znoa.domain.sys.UrlListTreeVo;
import com.dcy.znoa.service.sys.UrlListService;
import com.google.common.base.Strings;

@Service
public class UrlListServiceImpl implements UrlListService {

    @Autowired
    UrlListDao urlListDao;
    @Autowired
    OperRoleDao operRoleDao;
    @Autowired
    RoleAuthDao roleAuthDao;
    @Autowired
    OperDao userDao;
    
    @Override
    public JsonForList selectList(SelectUrlListModel model) throws InvocationTargetException, IllegalAccessException {
        List<UrlList> list;       
        
        EntityWrapper<UrlList> ew = new EntityWrapper<>();
        
        if(!Strings.isNullOrEmpty(model.getUrlName())){
            ew.eq("url_name",model.getUrlName());
        }
        if(!Strings.isNullOrEmpty(model.getUrlPath())){
            ew.eq("url_path",model.getUrlPath());
        }
        if(model.getValidFlag()!= null){
            ew.eq("valid_flag",model.getValidFlag());
        }

        //1 分页方法1，需要model继承 SelectBaseModel
        SelectBaseModel.PageFill(model,ew,urlListDao);//加入分页参数
        list = urlListDao.selectList(ew);
        //2 分页方法2 ，需要model里面包含 RowBounds 类，查询调用分页方法,但是 RowBounds里面的参数是MySQL 对应的，有点反人类，第一页要从0开始。
        //list = companyDao.selectPage(model.getRowBounds(),ew);
        if (list.isEmpty()) {
        	return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
        }
        return new JsonForList(list, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon insert(UrlList urlList) {
        Integer result = urlListDao.insert(urlList);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.INSERT_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @Override
    public JsonCommon updateById(UrlList urlList) {
        Integer result = urlListDao.updateById(urlList);
        if (result == null || result <= 0) {
            return new JsonCommon(JsonCommon.UPDATE_ERROR_CODE);
        }
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

	@Override
	public JsonCommon selectUrlListTreeByRoleId(Long roleId) {
		List<UrlListTreeVo> list = urlListDao.selectUrlListTreeByRoleId(roleId);
		if (!list.isEmpty()) {
			UrlListTreeVo root = new UrlListTreeVo();
			root.setUrlId(999999l);
			root.setUrlName("root");
			
			Long rootUrlId = 999999l;
			HashMap<Long, UrlListTreeVo> map = new HashMap<>();
			for (UrlListTreeVo vo : list)
			{
				map.put(vo.getUrlId(), vo);
				if (rootUrlId.longValue() == vo.getPreUrlId().longValue() )
				{
					root.getSubTree().add(vo);
				}
				else
				{
					UrlListTreeVo parent = map.get(vo.getPreUrlId());
					if (parent != null)
						parent.getSubTree().add(vo);
				}
			}
			return new JsonCommon(root, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}else {
			return new JsonCommon(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
		
	}

	@Override
	public JsonCommon selectUrlListTree() {
		List<UrlListTreeVo> list = urlListDao.selectUrlListTree();
		if (!list.isEmpty()) {
			UrlListTreeVo root = new UrlListTreeVo();
			root.setUrlId(999999l);
			root.setUrlName("root");

			Long rootUrlId = 999999l;
			HashMap<Long, UrlListTreeVo> map = new HashMap<>();
			for (UrlListTreeVo vo : list)
			{
				map.put(vo.getUrlId(), vo);
				if (rootUrlId.longValue() == vo.getPreUrlId().longValue() )
				{
					root.getSubTree().add(vo);
				}
				else
				{
					UrlListTreeVo parent = map.get(vo.getPreUrlId());
					if (parent != null)
						parent.getSubTree().add(vo);
				}
			}
			return new JsonCommon(root, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}else {
			return new JsonCommon(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
	}

	@Override
	public JsonCommon selectUrlListTreeByOperId(Long operId) {
		List<OperRole> operRoleList; 
//        EntityWrapper<OperRole> ew = new EntityWrapper<>();
//        ew.eq("oper_id",operId);
//        
//        //获取所属角色ID
//        operRoleList = operRoleDao.selectListMapper(ew);
		operRoleList = operRoleDao.selectOperRoleByOperId(operId);
		
		Oper oper = userDao.selectById(operId);
		Integer operType = oper.getOperType();
		
        List<Long> idList = new ArrayList<>();
        if (!operRoleList.isEmpty()) {
        	for (OperRole operRole : operRoleList) {
        		idList.add(operRole.getRoleId());
    		}
        }else if (operType.intValue() != 4) {
        	return new JsonCommon(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
        
        List<UrlListTreeVo> list = new ArrayList<>();
        if (!idList.isEmpty()) {
        	list = urlListDao.selectUrlListTreeByRoleIds(idList);
		}
		if (operType.intValue() == 4) {
			list = urlListDao.selectUrlListTree();

			for (Iterator iterator = list.iterator(); iterator.hasNext();) {
				UrlListTreeVo urlListTreeVo = (UrlListTreeVo) iterator.next();
				if (urlListTreeVo.getBelong().intValue() == 2) {
					iterator.remove();
				}				
			}
			
		}
		if (!list.isEmpty()) {
					
			UrlListTreeVo root = new UrlListTreeVo();
			root.setUrlId(999999l);
			root.setUrlName("root");
			
			Long rootUrlId = 999999l;
			HashMap<Long, UrlListTreeVo> map = new HashMap<>();
			for (UrlListTreeVo vo : list)
			{
					map.put(vo.getUrlId(), vo);
					if (rootUrlId.longValue() == vo.getPreUrlId().longValue() )
					{
						root.getSubTree().add(vo);
					}
					else
					{
						UrlListTreeVo parent = map.get(vo.getPreUrlId());
						if (parent != null)
							parent.getSubTree().add(vo);
					}
				
			}
			return new JsonCommon(root, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}else {
			return new JsonCommon(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
	}

	@Override
	public JsonForList selectUrlListByRoleId(Long roleId) {
        List<RoleAuth> list;       
        
        EntityWrapper<RoleAuth> ew = new EntityWrapper<>();
        ew.eq("role_id",roleId).eq("valid_flag", 1);
        
        //获取所属角色ID
        list = roleAuthDao.selectList(ew);
        
        if (!list.isEmpty()) {
        	List<Long> idList = new ArrayList<>();
        	for (RoleAuth roleAuth : list) {
        		idList.add(roleAuth.getUrlId());
    		}
        	
        	List<UrlList> urlList;     
        	//查询角色实体
        	urlList = urlListDao.selectBatchIds(idList);
        	if (urlList.isEmpty()) {
        		return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
			}else {
				return new JsonForList(urlList, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
			}
        	
		}else {
			return new JsonForList(null, JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}

	}


}
